<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="userId">
        <el-select
          v-model="queryParams.userId"
          placeholder="请选择"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in users"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          >
            <span style="float: left">{{ item.nickName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{
              item.dept
            }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="phone">
        <el-date-picker
          v-model="queryParams.startDate"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerStartOptions"
          @change="selectedStartDate"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="fax">
        <el-date-picker
          v-model="queryParams.endDate"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerEndOptions"
          @change="selectedEndDate"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="userTaskList">
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="开始时间" align="center" prop="startDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务总数" align="center" prop="taskCount">
        <template slot-scope="scope">
          <el-link type="primary" @click="taskList(scope.row, 'taskCount')">
            {{ scope.row.taskCount }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="已逾期" align="center" prop="overdue">
        <template slot-scope="scope">
          <el-link type="primary" @click="taskList(scope.row, 'overdue')">
            {{ scope.row.overdue }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="进行中" align="center" prop="doing">
        <template slot-scope="scope">
          <el-link type="primary" @click="taskList(scope.row, 'doing')">
            {{ scope.row.doing }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="已完成" align="center" prop="completed">
        <template slot-scope="scope">
          <el-link type="primary" @click="taskList(scope.row, 'completed')">
            {{ scope.row.completed }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="待考核" align="center" prop="awaitAssmt">
        <template slot-scope="scope">
          <el-link type="primary" @click="taskList(scope.row, 'awaitAssmt')">
            {{ scope.row.awaitAssmt }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="已考核" align="center" prop="assmted">
        <template slot-scope="scope">
          <el-link type="primary" @click="taskList(scope.row, 'assmted')">
            {{ scope.row.assmted }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="平均分" align="center" prop="scoreStr" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listTask } from "@/api/pms/project/taskreport";
import { listResources } from "@/api/pms/project/resources";
import moment from "moment";
export default {
  name: "Customer",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 用户任务
      userTaskList: [],
      //用户列表
      users: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        startDate: null,
        endDate: null,
      },
      pickerStartOptions: {
          // disabledDate(time) {
          //   return time.getTime() > Date.now();
          // },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }, {
            text: '一月前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', date);
            }
          }]
        },
        pickerEndOptions: {
          // disabledDate(time) {
          //   return time.getTime() > Date.now();
          // },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '明天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周后',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }, {
            text: '一月后',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24 * 30);
              picker.$emit('pick', date);
            }
          }]
        },
    };
  },
  created() {
    this.getList();
    listResources().then((response) => {
      this.users = response.data;
    });
  },
  methods: {
    formatDate(date) {
      return moment(date).format('YYYY-MM-DD');
    },
    selectedStartDate(data) {
      this.queryParams.startDate = this.formatDate(data);
    },
    selectedEndDate(data) {
      this.queryParams.endDate = this.formatDate(data);
    },
    taskList(row, type) {
      let params = {};
      params.userId = row.userId;
      params.type = type;
      params.startDate = this.queryParams.startDate
      params.endDate = this.queryParams.endDate
      this.$router.push({ path: "/rwgl/userTaskList", query: params });
    },
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then((response) => {
        this.userTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(e => {
        console.log(e)
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.queryParams.startDate = null;
      this.queryParams.endDate = null
      this.getList()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCustomer(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomer(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除客户编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCustomer(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "pms/customer/export",
        {
          ...this.queryParams,
        },
        `customer_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
